package com.antdesignbackend.backend.form.saleDeclarationB.service.impl;


import com.antdesignbackend.backend.activiti.constant.Constants;
import com.antdesignbackend.backend.activiti.processlist.entity.ProcessList;
import com.antdesignbackend.backend.activiti.processstay.entity.Processstay;
import com.antdesignbackend.backend.activiti.processstay.service.ProcessstayService;
import com.antdesignbackend.backend.activiti.reviewer.entity.Reviewer;
import com.antdesignbackend.backend.activiti.util.ActivitiUtil;
import com.antdesignbackend.backend.animalNumber.entity.AnimalNumber;
import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.entity.Production;
import com.antdesignbackend.backend.form.saleDeclarationA.entity.SaleDeclarationA;
import com.antdesignbackend.backend.service.ProductionService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antdesignbackend.backend.form.saleDeclarationB.mapper.SaleDeclarationBMapper;
import com.antdesignbackend.backend.form.saleDeclarationB.entity.SaleDeclarationB;
import com.antdesignbackend.backend.form.saleDeclarationB.service.SaleDeclarationBService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class SaleDeclarationBServiceImpl extends ServiceImpl<SaleDeclarationBMapper, SaleDeclarationB> implements SaleDeclarationBService {
    //表单标识
    private static final Integer Form = Constants.SALEDECLARATIONB;
    @Autowired
    ProcessstayService processstayService;
    @Autowired
    SaleDeclarationBService saleDeclarationBService;
    @Autowired
    AnimalNumberService animalNumberService;
    @Autowired
    ProductionService productionService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void submit(SaleDeclarationB saleDeclarationB) throws RuntimeException {
        if (saleDeclarationBService.save(saleDeclarationB)) {//保存成功
            approval(saleDeclarationB);
        } else {
            throw new RuntimeException("提交失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void updateSubmit(SaleDeclarationB saleDeclarationB) throws RuntimeException {
        if (saleDeclarationBService.updateById(saleDeclarationB)) {//保存成功
            approval(saleDeclarationB);
        } else {
            throw new RuntimeException("提交失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean updateB(SaleDeclarationB saleDeclarationB) {
        Boolean b = false;
        if (!(b = saleDeclarationBService.updateById(saleDeclarationB))){
            return b;
        }
        SaleDeclarationB saleDeclarationB1=saleDeclarationBService.getById(saleDeclarationB.getId());
        //修改动物明细记录
        UpdateWrapper<AnimalNumber> animalNumberUpdateWrapper=new UpdateWrapper<>();
        AnimalNumber animalNumber=animalNumberService.getById(saleDeclarationB1.getAid());   //旧明细
        if(saleDeclarationB1.getSaleAnimalType().equals("种公畜")){
            animalNumberUpdateWrapper.set("species",animalNumber.getSpecies()-saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("后备母畜")){
            animalNumberUpdateWrapper.set("reserve",animalNumber.getReserve()-saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("能繁母畜")){
            animalNumberUpdateWrapper.set("menstruation",animalNumber.getMenstruation()-saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("哺乳牲畜")){
            animalNumberUpdateWrapper.set("lactation",animalNumber.getLactation()-saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("保育牲畜")){
            animalNumberUpdateWrapper.set("conservation",animalNumber.getConservation()-saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("育成牲畜")){
            animalNumberUpdateWrapper.set("commodity",animalNumber.getCommodity()-saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("存栏家禽")){
            animalNumberUpdateWrapper.set("livestock",animalNumber.getLivestock()-saleDeclarationB1.getNumber());
        }
        animalNumberUpdateWrapper.eq("id",saleDeclarationB1.getAid());
        animalNumberService.update(animalNumberUpdateWrapper);
        //新增一条调出记录
        Production production=new Production();
        production.setEid(saleDeclarationB1.getEid());
        production.setChangeTime(saleDeclarationB1.getSaleDate());
        production.setSituation("调出");
        production.setUid(saleDeclarationB1.getCreateBy());
        production.setAnimalType(saleDeclarationB1.getAnimalType());
        if(saleDeclarationB1.getSaleAnimalType().equals("种公畜")){
            production.setMale(saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("后备母畜")){
            production.setFemale(saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("能繁母畜")){
            production.setBreedFemale(saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("哺乳牲畜")){
            production.setLactation(saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("保育牲畜")){
            production.setConservation(saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("育成牲畜")){
            production.setGrow(saleDeclarationB1.getNumber());
        }else if(saleDeclarationB1.getSaleAnimalType().equals("存栏家禽")){
            production.setNumber(saleDeclarationB1.getNumber());
        }
        production.setCreateTime(new Date());
        productionService.save(production);

        return b;
    }

    public void approval(SaleDeclarationB saleDeclarationB){
        //根据标识获得流程id
        Integer processId = ActivitiUtil.getProcessIdByForm(Form);
        if (processId == null)
            throw new RuntimeException("该流程不存在！");
        //获得下一个审批节点
        ProcessList processList = ActivitiUtil.getProcessList(processId,0);
        if (processList == null)
            throw new RuntimeException("审批不存在！");
        if (processList.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
            //获得下一个审批人
            Reviewer reviewer = ActivitiUtil.getReviewerNext(processList.getId(),0);
            if (reviewer == null)
                throw new RuntimeException("审批人不存在！");
            //创建待审记录
            Processstay processstay = new Processstay();
            processstay.setProcessId(processId);
            processstay.setForm(Form);
            processstay.setFormId(saleDeclarationB.getId());
            processstay.setProcesslistId(processList.getId());
            processstay.setTypes(processList.getTypes());
            processstay.setReviewerId(reviewer.getId());
            processstay.setUserId(reviewer.getReviewer());
            processstay.setLaunchId(saleDeclarationB.getCreateBy());
            processstay.setLaunchTime(saleDeclarationB.getCreateTime());
            processstayService.save(processstay);
        }else { //会签或或签
            //获得下一批审批人
            List<Reviewer> list = ActivitiUtil.getReviewer(processList.getId());
            if (list == null)
                throw new RuntimeException("审批人不存在！");
            for (Reviewer reviewer:list){
                //创建待审记录
                Processstay processstay = new Processstay();
                processstay.setProcessId(processId);
                processstay.setForm(Form);
                processstay.setFormId(saleDeclarationB.getId());
                processstay.setProcesslistId(processList.getId());
                processstay.setTypes(processList.getTypes());
                processstay.setReviewerId(reviewer.getId());
                processstay.setUserId(reviewer.getReviewer());
                processstay.setLaunchId(saleDeclarationB.getCreateBy());
                processstay.setLaunchTime(saleDeclarationB.getCreateTime());
                processstayService.save(processstay);
            }
        }
    }
}
